package com.travel.admin.util;

import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class StringUtil {
	public static String nvl(String str,String replaceChar){
		if(isEmpty(str)){
			return replaceChar;
		}
		return str.trim();
	}
	public static boolean isEmpty(String str){
		boolean flag = false;
		if(str == null || "".equals(str.trim()) || "null".equalsIgnoreCase(str) || "undefined".equalsIgnoreCase(str)){
			flag = true;
		}
		return flag;
	}
	public static String join(String[] strArray, String delimiter){
		if(strArray.length == 0){
			return "";
		}
		StringBuilder sb = new StringBuilder();
		int i = 0;
		for(i = 0; i < strArray.length - 1; i++){
			sb.append(strArray[i]).append(delimiter);
		}
		return sb.append(strArray[i]).toString();
	}
	/**
	 * 字符串去空格/回车/换行/制表符
	 * \n 回车(\u000a) 
	 * \t 水平制表符(\u0009) 
	 * \s 空格(\u0008) 
	 * \r 换行(\u000d)
	 * @param str
	 * @return
	 */
	public static String replaceBlank(String str){
		String dest = "";
		if (str != null) {
			Pattern p = Pattern.compile("\\s*|\t|\r|\n");
			Matcher m = p.matcher(str);
			dest = m.replaceAll("");
		}
		return dest;
	}
	public static String replaceSymbol(String str) {
		if(isEmpty(str)){
			return "";
		}
		str = str.trim().replaceAll("[\\pP‘’“”]", "");
		return str;
	}
	public static String subString(String str,int length){
		if(str.length() > length){
			return str.substring(0,length);
		}
		return str;
	}
	public static String join(List<String> list, String delimiter){
		int size = list.size();
		if(size == 0){
			return "";
		}
		StringBuilder sb = new StringBuilder();
		int i = 0;
		for(i = 0; i < size - 1; i++){
			sb.append(list.get(i)).append(delimiter);
		}
		return sb.append(list.get(i)).toString();
	}
	public static String join(Set<String> set, String delimiter){
		int size = set.size();
		if(size == 0){
			return "";
		}
		StringBuilder sb = new StringBuilder();
		boolean isFirst = true;
		for(String str : set){
			if(isFirst){
				sb.append(str);
				isFirst = false;
			}else{
				sb.append(delimiter).append(str);
			}
		}
		return sb.toString();
	}
	
	private static final boolean isChinese(char c) {
        Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);  
        if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS  
                || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS  
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A  
                || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION  
                || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION  
                || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {  
            return true;
        }
        return false;
    }
  
    public static final boolean hasChinese(String str) {  
        char[] ch = str.toCharArray();
        for (int i = 0; i < ch.length; i++) {
            char c = ch[i];
            if (isChinese(c)) {
                return true;
            }
        }
        return false;
    }
}